/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.MyConnectUnit;
import DTO.Department;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Quan_Nuce
 */
public class BDepartment {
    MyConnectUnit connect;
     public BDepartment() {
        this.connect = DAL.DAL.getDAL();
    }
        public HashSet<Department>getDepartments(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("Department",condition,OderBy);
        HashSet<Department> departments = new HashSet<Department>();
        while(result.next()){
            Department department = new Department();
            department.setDepartmentID(result.getInt("DepartmentID"));
            department.setDepartmentName(result.getString("DepartmentName"));
           department.setDescription(result.getString("Description"));
            
            
            departments.add(department);
        }
        return departments;
    }
          public HashSet<Department>getDepartments(String condition) throws Exception{
        return getDepartments(condition,null);
    }
  public HashSet<Department>getDepartments() throws Exception{
        return getDepartments(null);
    }
    
       public Department getByID(int id)throws Exception{
        HashSet<Department>departments = this.getDepartments("DepartmentID = "+ id);
        if(departments.size()>0)
            return departments.toArray(new Department[departments.size()])[0];
        return null;
       
   }
         public void Insert(Department department)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("DepartmentID", department.getDepartmentID());
        map.put("DepartmentName", department.getDepartmentName());
        map.put("Description", department.getDescription());
        
        this.connect.Insert("Department", map);
       
   }
         public void Inserts(HashSet<Department> departments)throws Exception{
        for(Department department:departments)
            this.Insert(department);
       
   }
          public void Update(Department department)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("DepartmentName", department.getDepartmentName());
        map.put("Description", department.getDescription());
       
        this.connect.Update("Department", map,"DepartmentID="+department.getDepartmentID());
       
   }
            public void Updates(HashSet<Department> departments)throws Exception{
        for(Department department:departments)
            this.Update(department);
}
      public void Delete(Department department) throws Exception{
        this.connect.Delete("Department", "DepartmentID="+department.getDepartmentID());
    }
       public void Deletes(HashSet<Department> departments) throws Exception {
        for(Department department:departments)
            this.Delete(department);
   }        
            
}